;; midi-note.scm - (c) rohan drape, 2000-2005

;; Evaluates to the frequency in cycles-per-second of the midi note
;; `p'.  This is the algorithm given by Puckette in Pd.

(define (midi-note->frequency p)
  (if (<= p -1500)
      0
      (if (> p 1499) 
	  (midi-note->frequency 1499)
	  (* 8.17579891564 (exp (* 0.0577622650 p))))))

;; Evaluates to the midi note equal to the cycles-per-second frequency
;; `f'.  The value is not exact, since some systems will accept real
;; midi note values as frequency values.  This is the algorithm given
;; by Puckette in Pd.

(define (frequency->midi-note f)
  (if (> f 0)
      (* 17.3123405046 (log (* 0.12231220585 f)))
      -1500))

;; This is the algorithm given by JMcC in SC3.

(define (midi-note->ratio midi)
  (expt 2 (* midi 0.083333333333)))

(define (ratio->midi-note ratio)
  (* 12 (log2 ratio)))
